<?php
/**注意此文档为UTF-8的编码.
 * Created on 2010-6-15
 *
 * @author EXT <isrn@qq.com>
 *
 */
include ('../../includes/global.php');
include ('../global.php');
include ("../power.php");
$dbcfg=Sys::Gp(array('dbcfg','sevenel'));
$items_array =array(
		array('name'=>'v20','value'=>'varchar(20)','type'=>' varchar(20) NOT NULL'),
		array('name'=>'v50','value'=>'varchar(50)','type'=>' varchar(50) NOT NULL'),
		array('name'=>'v100','value'=>'varchar(100)','type'=>' varchar(100) NOT NULL'),
		array('name'=>'int5','value'=>'int(5)','type'=>' int(5) NOT NULL'),
		array('name'=>'int10','value'=>'int(10)','type'=>' int(10) NOT NULL'),
		array('name'=>'time','value'=>'varchar(20)','type'=>' varchar(20) NOT NULL'),
		array('name'=>'text','value'=>'text','type'=>' text NOT NULL')
 );
 $items_needupindb = array('field_name','property');
 $items_config="{rows:" .json_encode($items_array)."}";
 $tableExist=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'sql'=>"show tables"
		  		));
 foreach($tableExist as $arraw=>$value){
		  		$table_exist[]=$value['Tables_in_cms'];
		  	}
$doNotCreate=array('id','del','lg','gid','sid','cid','tid','at','tt','ct','bdtid','bdid');
 switch($_REQUEST['require']){
 	case 'field_echo':{
 		$parent_id=$_POST['parent_id'];
		$tabname = gettabname($parent_id,$dbcfg['prefix']);
		$fullfields = Dgd(array(
		  'db'=>'d_sevenel',
		  'sql'=>"SHOW FULL FIELDS FROM ".$tabname
		  ));
		foreach ($fullfields as $key=>$value){
		if(!in_array($value['Field'],$doNotCreate)){
			$_fullfields[] =array('field_name'=>$value['Field'],'name'=>$value['Comment'],'property'=>$value['Type']);
		}
    	}
		 echo json_encode($_fullfields);
	break;}
 	case 'item_update':{
 		$id=$_POST['id'];
 		$parent_id=$_POST['parent_id'];
 		$field=$_POST['selectfield'];
 		$data=json_decode(stripslashes($_POST['data']),true);
 		if($data['name']==''){
					$comment='';
				}else{
					$comment=" COMMENT '".$data['name']."'";
				}
		$table=Dgd(array(
				  'db'=>'d_sevenel',
				  'cd'=>"id = $parent_id",
				  'tn'=>"datamod",
				  'limit'=>'1'
				  ));
		$tabname=$table['tabname'];
		$tabname = $dbcfg['prefix'].'_'.$table['tabname'];
		$type = getItemType($items_array,$data['property']);
			switch($field){
				case 'field_name':{
				$sql = "ALTER TABLE `".$tabname."` CHANGE `".$_POST['originalfield']."` `".$data['field_name']."`".$type.$comment;
				break;}
				default:{
				$sql = "ALTER TABLE `".$tabname."` CHANGE `".$_POST['originalfield']."` `".$data['field_name']."`".$type.$comment;}
			}
			$d_sevenel->Desql($sql);
 	break;}
 	case 'item_add':{
 		$tabname = gettabname($_POST['id'],$dbcfg['prefix']);
 		$field_name = $_POST['field_name'];
 		$fullfields = Dgd(array(
		  'db'=>'d_sevenel',
		  'sql'=>"SHOW FULL FIELDS FROM ".$tabname
		  ));
		foreach ($fullfields as $key=>$value){
		if($value['Field']==$field_name){
			ext_request_failure('已有该字段');
			exit();
		}
    	}
 		$commmend = getCommend($_POST['name']);
 		$type = getItemType($items_array,$_POST['property']);
 		$sql="ALTER TABLE `".$tabname."` ADD `".$field_name."`".$type.$commmend;
		$d_sevenel->Desql($sql);
		ext_request(json_encode($_POST));
 	break;}
 	case 'item_del':{
 		$tabname = gettabname($_POST['id'],$dbcfg['prefix']);
 		$field = $_POST['field'];
 		$sql = "ALTER TABLE `$tabname` DROP `$field`";
		$d_sevenel->Desql($sql);
		echo 'ok';
 	break;}
 	case 'item_zj':{
 		/*$tabname = gettabname($_POST['id'],$dbcfg['prefix']);
 		$field = $_POST['field'];
 		$sql = "ALTER TABLE `$tabname` DROP `$field`";
		$d_sevenel->Desql($sql);*/
		echo '考虑到一些现实的因素,这个功能暂时关闭!<br>Considering some reasons,This function temporarily shut down!';
 	break;}
 	case 'item_sy':{
		echo '考虑到一些现实的因素,这个功能暂时关闭!<br>Considering some reasons,This function temporarily shut down!';
 	break;}
 	case 'item_wy':{
		echo '考虑到一些现实的因素,这个功能暂时关闭!<br>Considering some reasons,This function temporarily shut down!';
 	break;}
 	case 'item_qw':{
		echo '考虑到一些现实的因素,这个功能暂时关闭!<br>Considering some reasons,This function temporarily shut down!';
 	break;}
 	case 'property':{
 		echo $items_config;
 	break;}
 	case 'property_update':{
 		$id=$_POST['id'];
 		$itemType=$_POST['itemType'];
		foreach($items_array as $key=>$value){
			if($value['name']==$itemType){
				$method=$value['method'];
			}
		}
		$save=array();
		foreach($method as $key_=>$value_){
				$save[]=array('name'=>$value_['name'],'value'=>$_POST[$value_['name']]);
		}
		$save=json_encode($save);
		if(Du('sevenel','fieldmod',array('property_value'=>$save),'id',$id,1)){
 				ext_request('ok');
 			}else{
 				echo '数据删除错误';
 			}
 	break;}
 	case 'field_create';{
 		$id=$_POST['id'];
 		if($id=='0'){
 			echo '请拖拽一个数据库再保存';
 		}else{
 			$table=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		  		));
		  	$field=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"parent_id = $id",
		  		'tn'=>"fieldmod"
		  		));
		  	$field!='' or die('一个字段都没？');
		  	$doNotCreate=array('id','del','lg','gid');
		  	$tablename=$dbcfg['prefix'].'_'.$table[0]['tabname'];
		  	$createline='';
  				foreach($field as $array=>$value){
  					if(in_array($value['fieldname'],$doNotCreate)){
					exit("'id','del','lg','gid'字段已经自动添加,不用重复添加!");
  					}else{
  						foreach($items_array as $array_=>$value_){
		  					if($value['property']==$value_['value']){
		  						$type=$value_['type'];
		  					}
		  				}
		  				$value['field_name']!='' or die('字段名没有输入');
		  				$type!='' or die('选择一个类型');
						$createline='ALTER TABLE `'.$tablename.'` ADD `'.$value['field_name'].'`'.' '.$type.'';
  					}
  				}
 		}

 	break;}
 	default:{
 	Ts('table/field_config.html');
	}
 }
?>
